//
//  HomePageViewController.m
//  SportGo
//
//  Created by rimi on 16/9/20.
//  Copyright © 2016年 Yrzteam. All rights reserved.
//

#import "HomePageViewController.h"
#import "HealthKitManage.h"
#import "CircleAnimationView.h"
#import "UICountingLabel.h"
#import "HWPopTool.h"
#import "RZContentView.h"
#import "RZSportsVC.h"
#import "WCAlertview.h"
#import "YJThirdSharePageView.h"
@interface HomePageViewController ()
@property (nonatomic,assign) BOOL flag;
@property (nonatomic, strong)CircleAnimationView *circleAniView;
@property (nonatomic, strong) UICountingLabel *myLabel;
@property (strong, nonatomic) UIView *contentView;
@property (nonatomic,strong) YJThirdSharePageView *yjThirdPage;
@end

@implementation HomePageViewController
- (void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES];
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"hh"]) {
        [self onClickBtn1];
        [self onClickBtn2];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self zhezhao];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(screen_size.width-50,30*bi, 40, 40);//设置尺寸
    [button setImage:[UIImage imageNamed:@"加号.png"] forState:0];
    [button addTarget:self action:@selector(action_changeTheState) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    self.tabBarController.tabBar.tintColor = [UIColor colorWithRed:58/255.0 green:200/255.0 blue:250/255.0 alpha:1];
    //显示tabbar
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openTheTabBar) name:@"openTheTabBar" object:nil];
   
    
    self.flag = YES;
    
    self.circleAniView = [[CircleAnimationView alloc] initWithFrame:CGRectMake(100,100, 200, 200)];
    self.circleAniView.center = self.view.center;
    self.circleAniView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.circleAniView];
    _myLabel = [[UICountingLabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.circleAniView.frame), CGRectGetHeight(self.circleAniView.frame))];
    
    _myLabel.textAlignment = NSTextAlignmentCenter;
    _myLabel.font = [UIFont boldSystemFontOfSize:30];
    _myLabel.textColor =getColor(58, 206, 248, 1);
  
    [self.circleAniView addSubview:_myLabel];
    //设置格式
    _myLabel.format = @"%d";
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(0,60, self.circleAniView.frame.size.width, 20);
 
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:@"Arial" size:13];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"今日步数";
    
    [self.circleAniView addSubview:label];
    UILabel *label1 = [[UILabel alloc]init];
    label1.frame = CGRectMake(0,140, self.circleAniView.frame.size.width, 20);
    label1.textColor = [UIColor whiteColor];
    label1.font = [UIFont fontWithName:@"Arial" size:13];
    label1.textAlignment = NSTextAlignmentCenter;
    label1.tag = 10;
    [self.circleAniView addSubview:label1];
    _yjThirdPage = [[YJThirdSharePageView alloc] initWithFrame:RzCGRectMake(0, 667, 375, 667)];
    [self.view addSubview:_yjThirdPage];

    
    UILabel *label2 = [[UILabel alloc]init];
    label2.frame = CGRectMake(0,120, self.circleAniView.frame.size.width, 20);
    
    label2.textColor = [UIColor whiteColor];
    label2.font = [UIFont boldSystemFontOfSize:12];
    label2.textAlignment = NSTextAlignmentCenter;
    label2.text = @"目标步数:10000";
    
    [self.circleAniView addSubview:label2];
    
   
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250*bi , 300*bi)];
    _contentView.backgroundColor = [UIColor clearColor];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(healthy) name:@"health" object:nil];
}

- (void)zhezhao{
    //遮罩  mask
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIColor colorWithRed:58/255.0 green:206/255.0 blue:248/255.0 alpha:1].CGColor,(__bridge id)[UIColor whiteColor].CGColor];
    gradientLayer.frame = self.view.bounds;
  
    CGFloat width = [UIScreen mainScreen].bounds.size.width;

     [self.view.layer addSublayer:gradientLayer];

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointZero];
    [path addLineToPoint:CGPointMake(0, 350*bi)];
    [path addQuadCurveToPoint:CGPointMake(width, 350*bi) controlPoint:CGPointMake(width/2, 550*bi)];
    [path addLineToPoint:CGPointMake(width, 350*bi)];
    [path addLineToPoint:CGPointMake(width, 0)];
    [path closePath];//直线关闭
    CAShapeLayer *shpe = [CAShapeLayer layer];
    shpe.frame = self.view.bounds;
    shpe.path = path.CGPath;
    shpe.strokeColor = [UIColor orangeColor].CGColor;
    
    gradientLayer.mask = shpe;
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(screen_size.width/2, 480*bi, 1, screen_size.height - 570*bi)];
    lineLabel.backgroundColor = getColor(58, 206, 248, 1);
    [self.view addSubview:lineLabel];
    
    UILabel *XLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 480*bi, 150*bi, 20)];
    XLabel.textColor = getColor(58, 206, 248, 1);
    XLabel.text = @"今日训练计划";
    XLabel.textAlignment = NSTextAlignmentCenter;
    XLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.view addSubview:XLabel];
    
    UIButton *Xbutton = [[UIButton alloc]initWithFrame:CGRectMake(screen_size.width/4-25, 520*bi, 50, 50)];
    [Xbutton setImage:[UIImage imageNamed:@"课程"] forState:0];
    [Xbutton addTarget:self action:@selector(action_plan) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:Xbutton];
    
    UIButton *Sbutton = [[UIButton alloc]initWithFrame:CGRectMake(screen_size.width/2+screen_size.width/4-30, 500*bi, 60, 60)];
    [Sbutton setImage:[UIImage imageNamed:@"开始"] forState:0];
    [Sbutton addTarget:self action:@selector(begin_train) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:Sbutton];
    
    
    
}
- (void)begin_train{
//开始训练
    RZSportsVC *rzSvc = [[RZSportsVC alloc]init];
    [self.navigationController pushViewController:rzSvc animated:YES];
}
- (void)action_plan{
    
    RZContentView *view = [[RZContentView alloc]initWithFrame:_contentView.bounds];
    view.layer.cornerRadius = 10;
    view.layer.masksToBounds = YES;
    [_contentView addSubview:view];
    [HWPopTool sharedInstance].shadeBackgroundType = ShadeBackgroundTypeSolid;
    [HWPopTool sharedInstance].closeButtonType = ButtonPositionTypeRight;
    [[HWPopTool sharedInstance] showWithPresentView:_contentView animated:YES];

}
- (void)onClickBtn1
{
    HealthKitManage *manage = [HealthKitManage shareInstance];
    [manage authorizeHealthKit:^(BOOL success, NSError *error) {
        
        if (success) {
        
            [manage getStepCount:^(double value, NSError *error) {
             
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    if ((int)value > 10000) {
                self.circleAniView.percentStr = @"10000" ;
            WCAlertview * alert = [[WCAlertview alloc]initWithTitle:@"提示" content:@"哇哦!你完成了目标步数,厉害" OkButton:@"确定"];
                [alert show];
    
                    }else{
                     self.circleAniView.percentStr = [NSString stringWithFormat:@"%d",(int)value] ;
                    }
                  
                   
                    //设置变化范围及动画时间
                    [_myLabel countFrom:0 to:(int)value withDuration:2.5];

                });
                
            }];
        }
        else {
            NSLog(@"fail");
        }
    }];
}

- (void)onClickBtn2
{
    HealthKitManage *manage = [HealthKitManage shareInstance];
    [manage authorizeHealthKit:^(BOOL success, NSError *error) {
        
        if (success) {
            
            [manage getDistance:^(double value, NSError *error) {
               
              
                dispatch_async(dispatch_get_main_queue(), ^{
                   
                    UILabel *label = [self.view viewWithTag:10];
                    label.text = [NSString stringWithFormat:@"行走公里数:%0.2f",value];
                    
                });
                
            }];
        }
        else {
            NSLog(@"fail");
        }
    }];
}


- (void)action_changeTheState{

    [UIView animateWithDuration:0.5 animations:^{
        
        _yjThirdPage.frame = RzCGRectMake(0, 0, 375, 667);
        
    }];
    self.tabBarController.tabBar.hidden = YES;
    
}


- (void)openTheTabBar{
   
    self.tabBarController.tabBar.hidden = NO;

}

- (void)healthy{
    [self onClickBtn1];
    [self onClickBtn2];
    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"hh"];
}
-(void)viewWillDisappear:(BOOL)animated{
[self.circleAniView clear];
self.circleAniView.percentStr = @"0";
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
